package com.amigo.online.provider.manager.util.video.processing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.amigo.online.provider.manager.model.entity.Video;
import com.amigo.online.provider.manager.util.param.dto.PageDto;
import com.amigo.online.provider.manager.util.video.processing.FFMPEG;
import com.amigo.online.provider.manager.util.video.size.ReadVideo;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;

/**
 * Unit test for simple App.
 */
public class VideoProcessingScript {

    public static void main(String[] args) throws Exception {

        videoManage2("E:\\videoResult");
        // getpicture("F:\\file2","F:\\file2\\photo");
        // getmusic("F:\\file3","F:\\music");
        // getjson("F:\\1587-1870修正版本");

        // getpicture("F:\\56","F:\\56\\photo");
        // interceptTheLengthOfVideo("C:\\Users\\Administrator\\Desktop\\tmp\\video\\IGotYou-BebeRexha.MP4",
        // "C:\\Users\\Administrator\\Desktop\\tmp\\originalVideo\\888.mp4");
        // videoManage2("F:\\1\\add处理包\\add");

    }

    /**
     * 只获取图片
     *
     * @param st3 视频文件夹 例：F:\\12
     * @param st4 图片存放路径 例: F:\\12\\picture
     * @throws IOException
     * @throws InterruptedException
     */
    public static void getpicture(String st3, String st4) throws IOException, InterruptedException {
        List<String> sslist = getAllFile(st3, false);
        for (String ssl : sslist) {
            System.out.println("mp4======" + ssl);
            String ssl2 = ssl.substring(0, ssl.length() - 4);
            System.out.println("视频名字=========" + ssl2);
            // 视频路径
            String videourl = st3 + "\\" + ssl;
            System.out.println("视频路径" + videourl);
            String pictureurl = st4 + "\\" + ssl2;
            System.out.println("图片存放路径" + pictureurl);
            chuangjian(videourl, pictureurl, ssl2);

        }

    }

    /**
     * 只获取音乐
     *
     * @param st3 视频路径
     * @param st4 音乐路径
     * @throws IOException
     * @throws InterruptedException
     */
    public static void getmusic(String st3, String st4) throws IOException, InterruptedException {
        List<String> sslist = getAllFile(st3, false);
        for (String ssl : sslist) {

            System.out.println("mp4======" + ssl);
            String ssl2 = ssl.substring(0, ssl.length() - 4);
            System.out.println("视频名字=========" + ssl2);
            // 视频路径
            String videourl = st3 + "\\" + ssl;
            System.out.println("视频路径" + videourl);
            String pictureurl = st4 + "\\" + ssl2;
            System.out.println("音乐存放路径" + pictureurl);
            music(videourl, pictureurl);
        }

    }

    /**
     * 综合处理视频
     *
     * @param videourl  视频所在文件夹 例如: D:\\image\\aaa
     * @param videourl2 处理后的视频文件夹例如： F:\\1\\1373-1518处理包\\1373-1518综合处理版本
     * @throws Exception
     */
    public static void videoManage(String videourl, String videourl2) throws Exception {

        String ss = videourl + "\\";
        Map<String, Video> map = new HashMap<>();

        List<String> list1 = readfilelist(videourl);
        for (String st : list1) {
            // 获取所有的文件夹
            // String st2 = st.substring(2, st.length());
            // 视频的绝对路径
            String ss2 = ss + st;
            System.out.println("st2========" + st);
            System.out.println("ss2==========" + ss2);
            // 创建文件夹
            String videofile = videourl2 + "\\" + st;
            File file = new File(videofile);
            file.mkdirs();
            // 获取文件夹下面的所有文件
            String st3 = ss2 + "\\";
            List<String> sslist = getAllFile(st3, false);
            for (String ssl : sslist) {
                System.out.println("ssl=========" + ssl);
                // mp4名称
                System.out.println("stt==========" + ssl);
                String ssl2 = ssl.substring(0, ssl.length() - 4);
                System.out.println("视频名字" + ssl2);

                String sssss = videofile + "\\" + ssl2;
                System.out.println("音乐图片存放地址" + sssss);
                // 原视频地址
                String url = ss2 + "\\" + ssl;
                // 处理后的视频地址
                String cuttervideo = videofile + "\\" + ssl;

                System.out.println("原视频地址:" + url);
                System.out.println("截取后的视频地址" + cuttervideo);
                // 截取视频
                cutvideo(url, cuttervideo);

                // 拷贝视频
                //convetor(url, cuttervideo);
                // 获取视频第一祯
                System.out.println("=====================");
                //map.put(ssl, chuangjian(cuttervideo, sssss, ssl));
                // 获取音频
                //music(cuttervideo, sssss);

            }

        }
        videotxt(JSON.toJSONString(map));

    }

    /**
     * 获取视频第一祯
     *
     * @param url   视频路径
     * @param name  图片存放地址
     * @param name2 图片名字
     * @throws IOException
     * @throws InterruptedException
     */
    public static Video chuangjian(String url, String name, String name2) throws IOException, InterruptedException {
        // String url = "D:\\image\\b\\compressedVideo\\" + name1 + ".mp4";
        // 获取视频时长
        PageDto pd = ReadVideo.videosize(url);
        File ff = new File(url);
        String size = ReadVideoTime(ff);
        System.out.println("-----------" + size.length());
        Video video = new Video();
        video.setVideoHeight(pd.getVideoHeight());
        video.setVideoWidth(pd.getVideoWidth());
        System.out.println("视频高+" + pd.getVideoHeight());
        System.out.println("视频宽+" + pd.getVideoWidth());
        video.setVideo(name2);
        System.out.println("视频时长" + size);
        video.setDuration(Integer.valueOf(size));
        System.out.println(name + "宽:" + pd.getVideoWidth() + "高:" + pd.getVideoHeight());
        String command = "E:\\lwl\\ffmpegUtil\\ffmpeg-20180714-c5329d6-win64-static\\bin\\ffmpeg.exe  -y -i " + '"'
                + url + '"' + " -vframes 1 -ss 00:00:00 -f mjpeg -s " + pd.getVideoWidth() + "*" + pd.getVideoHeight()
                + " -an " + '"' + name + ".jpg" + '"';
        System.out.println("获取视频第一帧命令:" + command);

        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();

        return video;
    }

    /**
     * 获取json
     *
     * @param url
     * @throws IOException
     * @throws FileNotFoundException
     * @throws InterruptedException
     */
    public static void getjson(String url) throws FileNotFoundException, IOException, InterruptedException {
        String ss = url + "\\";
        Map<String, Video> map = new HashMap<>();
        Map<String, Video> map2 = new HashMap<>();
        List<String> list = new ArrayList<>();
        List<String> list1 = readfilelist(url);
        int i = 0;
        for (String st : list1) {
            // 视频的绝对路径
            String ss2 = ss + st;
            // System.out.println("视频的绝对路径===" + ss2);

            // 获取文件夹下面的所有文件
            String st3 = ss2 + "\\";
            // System.out.println("st3======="+st3);
            List<String> sslist = getAllFile(st3, false);

            for (String ssl : sslist) {
                // System.out.println("ssl=========" + ssl);
                String smp4 = ssl.substring(ssl.length() - 4);
                if (smp4.equals(".MP4") || smp4.equals(".mp4")) {
                    String svideo = ss2 + "\\" + ssl;
                    // System.out.println("视频绝对路径==="+svideo);
                    PageDto pd = ReadVideo.videosize(svideo);
                    File ff = new File(svideo);
                    String size = ReadVideoTime(ff);
                    Video video = new Video();
                    video.setVideoHeight(pd.getVideoHeight());
                    video.setVideoWidth(pd.getVideoWidth());
                    video.setDestroy(Integer.valueOf(size));
                    System.out.println("视频名字=====" + ssl + "视频长度=====" + size + "视频宽===" + pd.getVideoWidth() + "视频高==="
                            + pd.getVideoHeight());
                    i = i + 1;
                    if (list.contains(ssl)) {
                        System.out.println("ssl===" + ssl);
                        String ssll = ssl;// 原名称
                        System.out.println("路径ss2===" + ss2);
                        String ss22 = ss2 + "\\";// 路径
                        System.out.println("视频绝对路径===" + svideo);
                        ssl = ssl.substring(0, ssl.length() - 4) + i + ssl.substring(ssl.length() - 4);// 修改后的视频名称
                        String sslll = ssl.substring(0, ssl.length() - 4);// 修改后的名称
                        System.out.println("修改后的视频名字" + ssl);

                        System.out.println(ssll + "+" + ss22 + "+" + ssl + "+" + sslll);
                        System.out.println("视频地址==" + ss22);
                        System.out.println("原名称===" + ssll.substring(0, ssll.length() - 4));
                        System.out.println("修改后的名称==" + sslll);
                        changename(ss22, ssll.substring(0, ssll.length() - 4), sslll);

                        video.setVideo(ssl);

                        map2.put(ssl, video);
                    }
                    list.add(ssl);
                    map.put(ssl, video);

                }

            }
        }
        System.out.println("map个数==" + map.keySet().size());
        System.out.println("视频个数==" + i);
        videotxt(JSON.toJSONString(map));
        // videotxt2(JSON.toJSONString(map2));
    }

    /**
     * 压缩视频
     *
     * @param name1
     * @param name2
     * @throws IOException
     * @throws InterruptedException
     */
    public static void yasuo(String name1, String name2) throws IOException, InterruptedException {

        String command = "E:\\lwl\\ffmpegUtil\\ffmpeg-20180714-c5329d6-win64-static\\bin\\ffmpeg.exe -i F:\\avideo\\"
                + name1 + ".mov -vcodec libx264  -b 1000k -y D:\\image\\b\\compressedVideo\\" + name2 + ".mp4";
        Process process = Runtime.getRuntime().exec(command);
        System.out.println("complite");
        process.waitFor();
    }

    /**
     * 获取音频
     *
     * @param url      视频路径
     * @param musicurl 音乐路径
     * @throws IOException
     * @throws InterruptedException
     */
    public static void music(String url, String musicurl) throws IOException, InterruptedException {

        String command = "E:\\lwl\\ffmpegUtil\\ffmpeg-20180714-c5329d6-win64-static\\bin\\ffmpeg.exe -i "
                + '"' + url + '"' + " -y -f mp3 -vn " + '"' + musicurl + ".mp3" + '"';
        System.out.println("获取音频命令:" + command);
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();
    }

    /**
     * 视频宽高添加到文本
     *
     * @param vidotxt
     */
    public static void videotxt(String vidotxt) {
        FileWriter fw = null;
        try {
            // 如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f = new File("D:\\image\\b\\videoTxt.json");
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(vidotxt);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 重复的添加到文本
     *
     * @param vidotxt
     */
    public static void videotxt2(String vidotxt) {
        FileWriter fw = null;
        try {
            // 如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f = new File("D:\\image\\b.json");
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(vidotxt);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取播放时长
     *
     * @param source
     * @return
     */
    private static String ReadVideoTime(File source) {
        Encoder encoder = new Encoder();
        String length = "";
        try {
            MultimediaInfo m = encoder.getInfo(source);

            long ls = m.getDuration() / 1000;
            // int hour = (int) (ls / 3600);
            // int minute = (int) (ls % 3600) / 60;
            // int second = (int) (ls - hour * 3600 - minute * 60);
            // length = hour + "'" + minute + "''" + second + "'''";
            length = String.valueOf(ls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }

    /**
     * 裁剪视频 url 原视频地址 cutterurl 裁剪后的视频地址
     *
     * @param video
     * @return
     */
    private static void cutvideo(String url, String cutterurl) {
        FFMPEG ff = new FFMPEG();
        PageDto pd = ReadVideo.videosize(url);
        HashMap<String, String> dto = new HashMap<String, String>();
        dto.put("ffmpeg_path", "E:\\lwl\\ffmpegUtil\\ffmpeg-20180714-c5329d6-win64-static\\bin\\ffmpeg.exe");// 必填
        dto.put("input_path", url);// 必填
        String width = String.valueOf(pd.getVideoWidth() * 0.81);
        String height = String.valueOf(pd.getVideoHeight() * 0.81);
        dto.put("width", width);
        dto.put("height", height);
        // String cutterurl="D:\\image\\b\\compressedVideo\\" + name + ".mp4";
        dto.put("video_converted_path", cutterurl);
        System.out.println(dto);
        ff.videoTransfer(dto);
    }

    /**
     * 拷贝视频
     *
     * @param url
     * @param cutterurl
     * @throws IOException
     * @throws InterruptedException
     */
    private static void copyvideo(String url, String cutterurl) throws IOException, InterruptedException {

        String command = "E:\\lwl\\ffmpegUtil\\ffmpeg-20180714-c5329d6-win64-static\\bin\\ffmpeg.exe -i " + url
                + "  -loglevel  trace  -c copy " + cutterurl + " -y";
        System.out.println("拷贝视频" + command);

        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();

    }

    /**
     * 读取文件夹
     *
     * @param filepath
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
        try {

            File file = new File(filepath);
            if (!file.isDirectory()) {
                System.out.println("文件");
                System.out.println("path=" + file.getPath());
                System.out.println("absolutepath=" + file.getAbsolutePath());
                System.out.println("name=" + file.getName());

            } else if (file.isDirectory()) {
                System.out.println("文件夹");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        System.out.println("path=" + readfile.getPath());
                        System.out.println("absolutepath=" + readfile.getAbsolutePath());
                        System.out.println("name=" + readfile.getName());

                    } else if (readfile.isDirectory()) {
                        readfile(filepath + "\\" + filelist[i]);
                    }
                }

            }

        } catch (FileNotFoundException e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        }
        return true;
    }

    /**
     * 获取所有的文件夹
     *
     * @param filepath 文件夹地址
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static List<String> readfilelist(String filepath) throws FileNotFoundException, IOException {
        File file = new File(filepath);
        System.out.println("文件夹");
        String[] filelist = file.list();
        List<String> ss = new ArrayList<>();
        for (int i = 0; i < filelist.length; i++) {
            File readfile = new File(filepath + "\\" + filelist[i]);
            if (!readfile.isDirectory()) {
                System.out.println("文件夹" + readfile.getName());
                ss.add(readfile.getName());

                // ss.add(readfile.getAbsolutePath());
            } else if (readfile.isDirectory()) {
                readfile(filepath + "\\" + filelist[i]);
                System.out.println("文件夹" + readfile.getName());
                ss.add(readfile.getName());
            }
        }
        return ss;
    }

    /**
     * 获取所有的文件
     *
     * @param directoryPath  文件夹地址
     * @param isAddDirectory 是否包含子文件夹
     * @return
     */
    public static List<String> getAllFile(String directoryPath, boolean isAddDirectory) {
        List<String> list = new ArrayList<String>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                if (isAddDirectory) {
                    list.add(file.getName());
                }
                list.addAll(getAllFile(file.getAbsolutePath(), isAddDirectory));
            } else {
                list.add(file.getName());
            }
        }
        return list;
    }

    /**
     * 修改名字
     *
     * @param url1 路径 例如D:\\image\\aa\\256-zj667788-盏姐5\\
     * @param url2 原名称
     * @param url3 修改后的名称
     * @throws IOException
     * @throws InterruptedException
     */
    public static void changename(String url1, String url2, String url3) throws IOException, InterruptedException {
        File file = new File(url1 + url2 + ".MP4");
        File file2 = new File(url1 + url2 + ".jpg");
        File file3 = new File(url1 + url2 + ".mp3");
        Thread.sleep(1000);
        file.renameTo(new File(url1 + url3 + ".MP4"));
        // Thread.sleep(1000);
        file2.renameTo(new File(url1 + url3 + ".jpg"));
        // Thread.sleep(1000);
        file3.renameTo(new File(url1 + url3 + ".mp3"));
    }

    /**
     * 截取视频长度
     *
     * @param url  原视频地址
     * @param url2 截取后的视频地址
     * @throws IOException
     * @throws InterruptedException
     * @throws InputFormatException
     * @throws EncoderException
     */
    public static void interceptTheLengthOfVideo(String url, String url2)
            throws IOException, InterruptedException, InputFormatException, EncoderException {

        String videoLength = getvideomessage(url);

        // String command =
        // "E:\\lwl\\ffmpegUtil\\ffmpeg-20180714-c5329d6-win64-static\\bin\\ffmpeg.exe
        // -i " + url
        // + " -vcodec copy -acodec copy -ss 00:00:00 -to 00:02:50 "+ url2 + " -y";
        String command = "E:\\lwl\\ffmpegUtil\\ffmpeg-20180714-c5329d6-win64-static\\bin\\ffmpeg.exe -ss 00:00:00 -t "
                + videoLength + " -accurate_seek -i " + url + " -codec copy -avoid_negative_ts 1 " + url2;

        System.out.println("获取视频第一帧命令:" + command);
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();

    }

    /**
     * 获取视频参数
     *
     * @param url 视频地址
     *            例如:C:\\Users\\Administrator\\Desktop\\tmp\\video\\recommend-失恋阵线联盟-原声带.mp4
     * @return
     * @throws IOException
     * @throws InputFormatException
     * @throws EncoderException
     */
    public static String getvideomessage(String url) throws IOException, InputFormatException, EncoderException {
        File source = new File(url);
        Encoder encoder = new Encoder();
        FileChannel fc = null;
        String size = "";

        it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(source);
        long ls = m.getDuration();
        // System.out.println("此视频时长为:" + ls / 60000 + "分" + (ls) / 1000 + "秒！");
        // 视频帧宽高
        // System.out.println("此视频高度为:" + m.getVideo().getSize().getHeight());
        // System.out.println("此视频宽度为:" + m.getVideo().getSize().getWidth());
        // System.out.println("此视频格式为:" + m.getFormat());
        @SuppressWarnings("resource")
        FileInputStream fis = new FileInputStream(source);
        fc = fis.getChannel();
        BigDecimal fileSize = new BigDecimal(fc.size());
        size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
        System.out.println("此视频大小为" + size);

        if (null != fc) {
            try {
                fc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 分钟
        Long fen = null;
        // 秒
        String mm = null;
        if ((ls) / 1000 > 60) {

            fen = ls / 60000;
            mm = String.valueOf(ls % 60000).substring(0, 2);
        } else {
            fen = (long) 00;
            mm = String.valueOf(ls % 60000).substring(0, 2);
        }
        System.out.println("此视频时长为:" + fen + "分" + mm + "秒");

        String videolength = "00:" + fen + ":" + (Integer.valueOf(mm) - 3);
        return videolength;
    }

    /**
     * 综合处理视频(截取视频长度)
     *
     * @param url2 视频所在文件夹 例如: D:\\image\\aaa
     * @throws IOException
     * @throws EncoderException
     * @throws InputFormatException
     */
    public static void videoManage2(String videourl)
            throws IOException, InterruptedException, InputFormatException, EncoderException {

        String ss = videourl + "\\";
        List<String> list1 = readfilelist(videourl);
        for (String st : list1) {
            // 获取所有的文件夹
            // String st2 = st.substring(2, st.length());
            // 视频的绝对路径
            String ss2 = ss + st;
            System.out.println("st2========" + st);
            System.out.println("ss2==========" + ss2);
            // 创建文件夹
            String videofile = "E:\\videoResult2\\" + st;
            File file = new File(videofile);
            file.mkdirs();
            // 获取文件夹下面的所有文件
            String st3 = ss2 + "\\";
            List<String> sslist = getAllFile(st3, false);
            for (String ssl : sslist) {
                System.out.println("ssl=========" + ssl);
                // mp4名称
                System.out.println("stt==========" + ssl);
                String ssl2 = ssl.substring(0, ssl.length() - 4);
                System.out.println("视频名字" + ssl2);

                String sssss = videofile + "\\" + ssl2;
                System.out.println("音乐图片存放地址" + sssss);
                // 原视频地址
                String url = ss2 + "\\" + ssl;
                // 处理后的视频地址
                String cuttervideo = videofile + "\\" + ssl;

                System.out.println("原视频地址:" + url);
                System.out.println("截取后的视频地址" + cuttervideo);
                // 截取视频
                interceptTheLengthOfVideo(url, cuttervideo);

            }

        }

    }

    private static Map<String, String> executeCmd(String cmd) {
        Runtime rt = Runtime.getRuntime(); // 运行时系统获取
        Map<String, String> lineMap = new HashMap<String, String>();// 存放返回值
        try {
            Process proc = rt.exec(cmd);// 执行命令
            InputStream stderr = proc.getInputStream();// 执行结果 得到进程的标准输出信息流
            InputStreamReader isr = new InputStreamReader(stderr);// 将字节流转化成字符流
            BufferedReader br = new BufferedReader(isr);// 将字符流以缓存的形式一行一行输出
            String line = null;
            while ((line = br.readLine()) != null) {
                if (!StringUtils.isEmpty(line)) {
                    String[] strLine = line.split(":");
                    if (strLine.length >= 2) {
                        lineMap.put(strLine[0].trim(), strLine[1].trim());
                    }

                }
            }
            br.close();
            isr.close();
            stderr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lineMap;
    }

    /**
     * 拷贝视频2
     *
     * @param videoInputPath 视频的输入路径
     * @param videoOutPath   视频的输出路径
     * @throws Exception
     */
    // 拷贝视频，并指定新的视频的名字以及格式
    // ffmpeg.exe -i old.mp4 new.avi
    public static void convetor(String videoInputPath, String videoOutPath) throws Exception {

        List<String> command = new ArrayList<String>();
        command.add("E:\\lwl\\ffmpegUtil\\ffmpeg-20180714-c5329d6-win64-static\\bin\\ffmpeg.exe");
        command.add("-i");
        command.add(videoInputPath);
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            //  Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }

}
